package com.ball.util;
import com.ball.service.impl.WalletServiceImpl;
import com.ball.tools.StringUtil;
import com.alibaba.fastjson.JSONObject;
import com.ball.entity.User;
import com.ball.service.UserService;
import com.ball.service.WalletService;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

import static com.ball.tools.StringUtil.isNull;

public class WalletUtil{

    public static void main(String[] args) {
        List<String> list=new ArrayList<String>();
        list.add("cd /root/stardustcoin/build/src");
//       list.add("ls");
//       list.add("./simplewallet --wallet-file 123123123 --password \"\" --command address");
        list.add("./simplewallet --wallet-file 123123123 --password \"\" ");
        list.add("transfer 1 fySMZPTaJQtXWXRw1Pwi27gsnJ2biXb88Wrgtk8HizTM5RxJazzkArXTBoG9z257BkJ7Y4Y4qRC3wFGRevAydjVe2qZcp16Yd 1");
//       list.add("exit");
//        list.add("ps -ef");
        System.out.println(executeNewFlow(list,"[wallet"));


    }
    //List size=3
    public static List<String> executeNewFlow (List<String> commands,String endWords)  {
        TestThread1 testThread1=new TestThread1(commands,endWords);
        Thread thread=new Thread(testThread1);
        thread.setPriority(10);
        thread.start();
        do{

        }while (thread.isAlive());
        return testThread1.getRspList();
    }
    //List size=4orList size=3
    public static List<String> executeNewFlow(List<String> commands,String endWords1,String endWords2) {
        TestThread2 testThread2=new TestThread2(commands,endWords1,endWords2);
        Thread thread=new Thread(testThread2);
        thread.setPriority(10);
        thread.start();
        do{

        }while (thread.isAlive());
        return testThread2.getRspList();
    }
    //List size=4orList size=3orList size=5
    public static List<String> executeNewFlow(List<String> commands,String endWords1,String endWords2,String endWords3) {
        TestThread3 testThread3=new TestThread3(commands,endWords1,endWords2,endWords3);
        Thread thread=new Thread(testThread3);
        thread.setPriority(10);
        thread.start();
        do{

        }while (thread.isAlive());
        return testThread3.getRspList();
}
    static class TestThread1 implements Runnable {
    private List<String> commands;
    private String endWords;
    private List<String> rspList = new ArrayList<String>();
    public TestThread1(List<String> commands, String endWords) {
        this.commands = commands;
        this.endWords = endWords;
    }
    public void run()
    {
        rspList.add("null");
        Runtime run = Runtime.getRuntime();
        try {

            java.lang.Process proc = run.exec("/bin/bash", null, null);
            BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
            PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(proc.getOutputStream())), true);
            out.println(commands.get(0));
            System.out.println("0");
            out.println(commands.get(1));
            System.out.println(commands.get(1));
            System.out.println("0");
            // out.println("cd /home/test");
            // out.println("pwd");
            // out.println("rm -fr /home/proxy.log");
//            out.println("exit");// 这个命令必须执行，否则in流不结束。
            String rspLine = "";
            int judgeIf=0;
            while ((rspLine = in.readLine()) != null) {
                System.out.println(rspLine);
                if((rspList.get(0).indexOf("Height")!=-1)||(rspLine.indexOf("Height")!=-1)) {
                    if(rspList.get(0).equals("null")){
                        rspList.set(0,rspLine);
                    }
                    if (rspLine.indexOf("Height") != -1&&judgeIf==0) {
                        judgeIf++;
                        out.println(commands.get(2));
                    }
                    if (rspLine.indexOf(endWords) != -1) {
//                        out.println(commands.get(3));
                        rspList.add(rspLine);
                        return ;
                    }
                    //                rspList.add(rspLine);
                    if (rspLine.indexOf("Wallet closed") != -1) {
                        out.println("exit");
                        System.out.println("exit");
                        rspList.add(rspLine);
                        return ;
                    }
                }
            }
            proc.waitFor();
            in.close();
            out.close();
            proc.destroy();
        } catch (IOException e1) {
            e1.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return ;
    }

    public List<String> getRspList() {
        return rspList;
    }

    public void setRspList(List<String> rspList) {
        this.rspList = rspList;
    }
}
    static class TestThread2 implements Runnable {
    private List<String> commands;
    private String endWords1;
    private String endWords2;
    private List<String> rspList = new ArrayList<String>();
    public TestThread2(List<String> commands, String endWords1,String endWords2) {
        this.commands = commands;
        this.endWords1 = endWords1;
        this.endWords2 = endWords2;
    }
    public void run()
    {
        rspList.add("null");
        Runtime run = Runtime.getRuntime();
        try {
            java.lang.Process proc = run.exec("/bin/bash", null, null);
                BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
                PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(proc.getOutputStream())), true);
                out.println(commands.get(0));
                out.println(commands.get(1));
                System.out.println("0");
                System.out.println(commands.get(1));
                System.out.println("0");
                System.out.println("0");
                // out.println("cd /home/test");
                // out.println("pwd");
                // out.println("rm -fr /home/proxy.log");

                String rspLine = "";
                int judgeIf=0;
                while ((rspLine = in.readLine()) != null) {
                    System.out.println(rspLine);
                    if((rspList.get(0).indexOf("Height")!=-1)||(rspLine.indexOf("Height")!=-1)) {
                        if(rspList.get(0).equals("null")){
                            rspList.set(0,rspLine);
                        }
                        if (rspLine.indexOf("Height") != -1&&judgeIf==0) {
                            judgeIf++;
                            out.println(commands.get(2));
                        }
                        if (rspLine.indexOf(endWords1) != -1) {
//                        out.println(commands.get(3));
                            rspList.add(rspLine);
                            return ;
                        }
                        if (rspLine.indexOf(endWords2) != -1) {
//                        out.println(commands.get(4));
                            rspList.add(rspLine);
                            return ;
                        }
                        //                rspList.add(rspLine);
                        if (rspLine.indexOf("Wallet closed") != -1) {
                            out.println("exit");
                            rspList.add(rspLine);
                            System.out.println("exit");
                            return ;
                        }
                    }
                }
                proc.waitFor();
                in.close();
                out.close();
                proc.destroy();
            } catch (IOException e1) {
                e1.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return ;
    }

    public List<String> getRspList() {
        return rspList;
    }

    public void setRspList(List<String> rspList) {
        this.rspList = rspList;
    }
}
    static class TestThread3 implements Runnable {
    private List<String> commands;
    private String endWords1;
    private String endWords2;
    private String endWords3;
    private List<String> rspList = new ArrayList<String>();

    public TestThread3(List<String> commands, String endWords1, String endWords2, String endWords3) {
        this.commands = commands;
        this.endWords1 = endWords1;
        this.endWords2 = endWords2;
        this.endWords3 = endWords3;
    }

    public void run() {
        rspList.add("null");
        Runtime run = Runtime.getRuntime();
        try {
            java.lang.Process proc = run.exec("/bin/bash", null, null);
            BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
            PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(proc.getOutputStream())), true);
            out.println(commands.get(0));
            out.println(commands.get(1));
            System.out.println("0");
            System.out.println(commands.get(1));
            System.out.println("0");
            System.out.println("0");
            // out.println("cd /home/test");
            // out.println("pwd");
            // out.println("rm -fr /home/proxy.log");

            String rspLine = "";
            int judgeIf = 0;
            while ((rspLine = in.readLine()) != null) {
                System.out.println(rspLine);
                if ((rspList.get(0).indexOf("Height") != -1) || (rspLine.indexOf("Height") != -1)) {
                    if (rspList.get(0).equals("null")) {
                        rspList.set(0, rspLine);
                    }
                    if (rspLine.indexOf("Height") != -1 && judgeIf == 0) {
                        judgeIf++;
                        out.println(commands.get(2));
                    }
                    if (rspLine.indexOf(endWords1) != -1) {
                        out.println(commands.get(3));
                        rspList.add(rspLine);
                    }
                    if (rspLine.indexOf(endWords2) != -1) {
                        out.println(commands.get(4));
                        rspList.add(rspLine);
                    }
                    if (rspLine.indexOf(endWords3) != -1) {
                        out.println(commands.get(5));
                        rspList.add(rspLine);
                        return;

                    }
                    //                rspList.add(rspLine);
                    if (rspLine.indexOf("Wallet closed") != -1) {
                        out.println("exit");
                        rspList.add(rspLine);
                        System.out.println("exit");
                        return;
                    }
                }
            }
            proc.waitFor();
            in.close();
            out.close();
            proc.destroy();
        } catch (IOException e1) {
            e1.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return;
    }

    public List<String> getRspList() {
        return rspList;
    }

    public void setRspList(List<String> rspList) {
        this.rspList = rspList;
    }
}
}